2 Copyright (C) 2024 Rubén Beltrán del Río
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see https://map.tranquil.systems.
20 class MapTextEditorController: NSViewController {
22 @Binding var document: MapDocument
23 let onChange: () -> Void
25 private let vertexRegex = MapParsingPatterns.vertex
26 private let edgeRegex = MapParsingPatterns.edge
27 private let blockerRegex = MapParsingPatterns.blocker
28 private let opportunityRegex = MapParsingPatterns.opportunity
29 private let noteRegex = MapParsingPatterns.note
30 private let stageRegex = MapParsingPatterns.stage
31 private let groupRegex = MapParsingPatterns.group
33 private let changeDebouncer: Debouncer = Debouncer(seconds: 1)
35 init(document: Binding<MapDocument>, onChange: @escaping () -> Void) {
36 self._document = document
37 self.onChange = onChange
38 super.init(nibName: nil, bundle: nil)
41 required init?(coder: NSCoder) {
42 fatalError("init(coder:) has not been implemented")
45 override func loadView() {
46 let scrollView = NSTextView.scrollableTextView()
47 let textView = scrollView.documentView as! NSTextView
49 scrollView.translatesAutoresizingMaskIntoConstraints = false
51 textView.backgroundColor = .ui.background
52 textView.allowsUndo = true
53 textView.delegate = self
54 textView.textStorage?.delegate = self
55 textView.string = self.document.text
56 textView.isEditable = true
57 textView.font = .monospacedSystemFont(ofSize: 16.0, weight: .regular)
58 self.view = scrollView
61 override func viewDidAppear() {
62 self.view.window?.makeFirstResponder(self.view)
66 extension MapTextEditorController: NSTextViewDelegate {
68 func textDidChange(_ obj: Notification) {
69 if let textField = obj.object as? NSTextView {
70 self.document.text = textField.string
72 changeDebouncer.debounce {
73 DispatchQueue.main.async {
80 func textView(_ view: NSTextView, shouldChangeTextIn: NSRange, replacementString: String?) -> Bool
82 let range = Range(shouldChangeTextIn, in: view.string)
83 let target = view.string[range!]
93 extension MapTextEditorController: NSTextStorageDelegate {
95 override func textStorageDidProcessEditing(_ obj: Notification) {
96 if let textStorage = obj.object as? NSTextStorage {
97 self.colorizeText(textStorage: textStorage)
101 private func colorizeText(textStorage: NSTextStorage) {
102 let range = NSMakeRange(0, textStorage.length)
103 var matches = vertexRegex.matches(in: textStorage.string, options: [], range: range)
105 for match in matches {
106 textStorage.addAttributes(
107 [.foregroundColor: NSColor.syntax.vertex], range: match.range(at: 1))
108 textStorage.addAttributes(
109 [.foregroundColor: NSColor.syntax.number], range: match.range(at: 2))
110 textStorage.addAttributes(
111 [.foregroundColor: NSColor.syntax.number], range: match.range(at: 3))
112 textStorage.addAttributes(
113 [.foregroundColor: NSColor.syntax.option], range: match.range(at: 4))
116 matches = edgeRegex.matches(in: textStorage.string, options: [], range: range)
118 for match in matches {
119 textStorage.addAttributes(
120 [.foregroundColor: NSColor.syntax.vertex], range: match.range(at: 1))
121 let arrowRange = match.range(at: 2)
122 textStorage.addAttributes(
123 [.foregroundColor: NSColor.syntax.symbol],
124 range: NSMakeRange(arrowRange.lowerBound - 1, arrowRange.length + 1))
125 textStorage.addAttributes(
126 [.foregroundColor: NSColor.syntax.vertex], range: match.range(at: 3))
129 matches = opportunityRegex.matches(in: textStorage.string, options: [], range: range)
131 for match in matches {
132 textStorage.addAttributes(
133 [.foregroundColor: NSColor.syntax.option], range: match.range(at: 1))
134 textStorage.addAttributes(
135 [.foregroundColor: NSColor.syntax.vertex], range: match.range(at: 2))
136 textStorage.addAttributes(
137 [.foregroundColor: NSColor.syntax.symbol], range: match.range(at: 3))
138 textStorage.addAttributes(
139 [.foregroundColor: NSColor.syntax.number], range: match.range(at: 4))
142 matches = blockerRegex.matches(in: textStorage.string, options: [], range: range)
144 for match in matches {
145 textStorage.addAttributes(
146 [.foregroundColor: NSColor.syntax.option], range: match.range(at: 1))
147 textStorage.addAttributes(
148 [.foregroundColor: NSColor.syntax.vertex], range: match.range(at: 2))
151 matches = noteRegex.matches(in: textStorage.string, options: [], range: range)
153 for match in matches {
154 textStorage.addAttributes(
155 [.foregroundColor: NSColor.syntax.option], range: match.range(at: 1))
156 textStorage.addAttributes(
157 [.foregroundColor: NSColor.syntax.number], range: match.range(at: 2))
158 textStorage.addAttributes(
159 [.foregroundColor: NSColor.syntax.number], range: match.range(at: 3))
162 matches = stageRegex.matches(in: textStorage.string, options: [], range: range)
164 for match in matches {
165 textStorage.addAttributes(
166 [.foregroundColor: NSColor.syntax.option], range: match.range(at: 1))
167 textStorage.addAttributes(
168 [.foregroundColor: NSColor.syntax.number], range: match.range(at: 2))
171 matches = groupRegex.matches(in: textStorage.string, options: [], range: range)
173 for match in matches {
174 textStorage.addAttributes(
175 [.foregroundColor: NSColor.syntax.option], range: match.range(at: 1))
176 textStorage.addAttributes(
177 [.foregroundColor: NSColor.syntax.vertex], range: match.range(at: 2))
182 struct MapTextEditor: NSViewControllerRepresentable {
184 @Binding var document: MapDocument
185 var onChange: () -> Void = {}
187 func makeNSViewController(
188 context: NSViewControllerRepresentableContext<MapTextEditor>
189 ) -> MapTextEditorController {
190 return MapTextEditorController(document: $document, onChange: onChange)
193 func updateNSViewController(
194 _ nsViewController: MapTextEditorController,
195 context: NSViewControllerRepresentableContext<MapTextEditor>